dojo.provide("xray.tools.ActivityGrid");

dojo.require("dijit._Widget");

dojo.registerModulePath("xray.images", "../../images");
dojo.requireLocalization("xray.tools", "ActivityGrid");

dojo.declare("xray.tools.ActivityGrid",
	[dijit._Widget], {
		
	constructor:function(data){
		this._data = data;
	},
	
	postCreate: function(){
		this.messages = dojo.i18n.getLocalization("xray.tools", "ActivityGrid", this.lang);
		this.messages.id = this.id;
		
		this.initGrid();
		
		dojo.subscribe("xray/data/new", this, "addData");
	},
	
	// initializes datagrid
	initGrid: function(){
		// create grid to display data
		this.activityStore = new dojo.data.ItemFileWriteStore({data: {items: []}});

		var layout = [[
			{
				name: this.messages.time,
				field: 'dateTime',
				width: "15%",
				formatter:function(item){ return item.replace(" ", '<span style="font-size:8px;">  ') + '</span>'; }
			},
			{name: this.messages.cpu, field: 'cpu', width: "10%"},
			{name: this.messages.memory, field: 'memory', width: "10%"},
			{name: this.messages.category, field: 'logCategory', width: "20%"},
			{name: this.messages.log, field: 'logMessage', width: "auto"}
		]];

		this.grid = new xray.ActivityGrid({
			store: this.activityStore, 
			structure: layout, 
			rowsPerPage: 10, 
			style: "height: 100%; width: 100%; overflow: hidden;",
			region: "center"
		}, dojo.doc.createElement("div"));
		
		dojo.byId("activityGrid").appendChild(this.grid.domNode);
		this.grid.startup();

		// connecting to grid events
		// loading, editing
		dojo.connect(this.grid, "onCellClick", this, function(e){
			
		});
	},
	
	addData: function(timestamp){
		var data = this._data.get(timestamp);
		dojo.forEach(data, function (item){
			if (!item.log.type) return; // Write only events.
//console.log(dojo.toJson(item));
			var time = new Date(item.timestamp);
			var hrs = time.getHours(), min = time.getMinutes(), sec = time.getSeconds(), ms = time.getMilliseconds();
			var row = {
				dateTime:(hrs<10?"0"+hrs:hrs) + ":" + (min<10?"0"+min:min) + ":" + (sec<10?"0"+sec:sec) + " " + time.getMilliseconds(),
				cpu:Math.round(item.cpu*100) / 100, // Format it to have two after comma digits.
				memory:Math.round(item.memory/10.24)/100,
				logCategory:item.log.type,
				logMessage:item.log.message
			};
			this.activityStore.newItem(row,{index : 0});
			this.activityStore.save();
		}, this);
	},
	
	resizeGrid: function(){
		dojo.style(this.grid.domNode, "height", "100%");
	}
});

// this is a temporary fix until dojo grid supports adding rows to the beginning
dojo.declare("xray.ActivityGrid", xray.Grid, {
	_addItem: function(item, index, noUpdate){
		index = 0;
		var idty = this._hasIdentity ? this.store.getIdentity(item) : dojo.toJson(this.query) + ":idx:" + index + ":sort:" + dojo.toJson(this.getSortProps());
		var o = { idty: idty, item: item };
		//this._by_idty[idty] = this._by_idx[index] = o;
		dojox.grid.util.arrayInsert(this._by_idx,index,o); 
	 	this._by_idty[idty] =  o;
	
		if(!noUpdate){
			this.updateRow(index);
		}
	},

	_onNew: function(item, parentInfo){
		var indexToPut = this.rowCount; 
		if(parentInfo != undefined && parentInfo.index!= undefined){ 
			indexToPut = parentInfo.index; 
		}
		
		this.updateRowCount(this.rowCount+1);
		this._addItem(item,indexToPut); 
		this.updateRows(indexToPut +1, this.rowCount - indexToPut); 
		this.showMessage();
	}
})